<?php

namespace api\modules\manage\controllers;

use api\traits\ControllerTrait;
use Yii;
use yii\base\InvalidConfigException;
use yii\filters\Cors;
use yii\web\Response;

abstract class BaseController extends \yii\rest\ActiveController
{
    use ControllerTrait;

    public $serviceClass;
    protected $formatType;
    protected $service;

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        if ($this->serviceClass === null) {
            throw new InvalidConfigException('The "serviceClass" property must be set.');
        }
        $this->getService();
    }

    private function getService()
    {
        $id = intval($this->getQuery('id'));
        $method = Yii::$app->request->getMethod();
        if($method == 'GET'){
            $params = $this->getQuery();
        }else{
            $params = $this->getParams();
        }
        $this->service = Yii::createObject(['class' => $this->serviceClass, 'id' => $id, 'params' => $params]);
    }

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors = array_merge(['cors' => [
            'class' => Cors::className(),
        ]], $behaviors);
        if (Yii::$app->getRequest()->getMethod() !== 'OPTIONS') {
            $this->attachBehavior('checkAuth', new \api\components\behaviors\BaseBehavior());
        }
        Yii::$app->response->format = Response::FORMAT_JSON;

        return $behaviors;
    }

    public function beforeAction($action)
    {
        $method = Yii::$app->getRequest()->getMethod();
        if ($method !== 'OPTIONS') {
            // BaseBehavior.php
            if($this->check()){
                if($this->formatType){
                    if($method == 'GET'){
                        $type = $this->getQuery('type');
                    }else{
                        $type = $this->getParams('type');
                    }
                }
                return true;
            }
        }

        return true;
    }

    protected function verbs()
    {
        return [
            'index' => ['GET', 'HEAD'],
            'view' => ['GET', 'HEAD'],
            'create' => ['POST'],
            'update' => ['PUT'],
            'delete' => ['DELETE'],
            'switch' => ['PATCH']
        ];
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'],$actions['create'],$actions['update'],$actions['delete'],$actions['view']);
        return $actions;
    }

    public function actionIndex()
    {
        try {
            $data = $this->service->search(true);
            return $this->makeApiData([$data],[]);
        }catch (\Exception $e){
            return $this->makeApiData([],['msg' => $e->getMessage()]);
        }
    }

    public function actionView()
    {
        try {
            $result = $this->service->view();
            return $this->makeApiData(['data' => $result],[]);
        }catch (\Exception $e){
            return $this->makeApiData([],['msg' => $e->getMessage()]);
        }
    }

    public function actionCreate()
    {
        try {
            $result = $this->service->create();
            return $this->makeApiData(['data' => $result],[]);
        }catch (\Exception $e){
            return $this->makeApiData([],['msg' => $e->getMessage()]);
        }
    }

    public function actionUpdate()
    {
        try {
            $result = $this->service->update();
            return $this->makeApiData(['data' => $result],[]);
        }catch (\Exception $e){
            return $this->makeApiData([],['msg' => $e->getMessage()]);
        }
    }

    public function actionDelete()
    {
        try {
            $result = $this->service->delete();
            return $this->makeApiData(['data' => $result],[]);
        }catch (\Exception $e){
            return $this->makeApiData([],['msg' => $e->getMessage()]);
        }
    }

    public function actionSwitch()
    {
        try {
            $result = $this->service->change();
            return $this->makeApiData(['data' => $result],[]);
        }catch (\Exception $e){
            return $this->makeApiData([],['msg' => $e->getMessage()]);
        }
    }
}